﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MB.Orm.Common;
using System.Web;
using Newtonsoft.Json;
using Model;
using Business;
using WoChu.Autorun.Base.Model;
using System.Data;

namespace WebApi.Controllers
{
    public class OrderPickController : ApiController
    {

        [HttpGet]
        public ResultModel GetReviewInfo(string BoxNO)
        {
            MB.Util.TraceEx.Write(BoxNO);
            try
            {
                List<WCS_TO_WMS> lstWCS_TO_WMS = new BOrderPick().GetWCS_TO_WMSlistByBoxNo(BoxNO);
                if (lstWCS_TO_WMS == null || lstWCS_TO_WMS.Count <= 0)
                {
                    List<WMS_TO_WCS_TASK> tasklst = new BOrderPick().GetWMS_TO_WCS_TASKlistByZBoxNo(BoxNO);
                    if (tasklst == null || tasklst.Count <= 0)
                    {
                        return new ResultModel { RESULT = "failed", MESSAGE = "获取复核信息失败：周转箱未绑定订单", DATA = null };
                    }
                    else
                    {
                        string FboxNO = "";
                        string OrdrNO = "";
                        string PickTime = "";
                        foreach (WMS_TO_WCS_TASK ite in tasklst)
                        {
                            OrdrNO = ite.ORDERNO;
                            PickTime = ite.CREATE_DATE.ToString();
                            FboxNO += "【" + ite.FBOXNO + "】";
                        }
                        return new ResultModel { RESULT = "failed", MESSAGE = "周转箱最新一笔核单数据》》周转箱：" + BoxNO + "--订单号：" + OrdrNO + "--发货箱：" + FboxNO + "--拣货完成时间：" + PickTime, DATA = null };
                    }
                }
                string OrderNo = lstWCS_TO_WMS.FirstOrDefault().ORDERNO;
                List<WCS_TO_WMS> lstWCS_TO_WMS1 = new List<WCS_TO_WMS>();
                lstWCS_TO_WMS1 = new BOrderPick().GetWCS_TO_WMSlistByOrderNo(OrderNo);//拣货完成信息
                DataSet ds = new BOrderPick().GetSaleOrderItemlistByOrderNo(OrderNo);//ERP订单信息

                //var erplistWCS_TO_WMS =(from DataRow dr in ds.Tables[0].Rows
                //         select new WCS_TO_WMS {
                //             ITEMCODE = dr["goodscode"].ToString(),
                //             ITEMNAME = dr["goodsname"].ToString()
                //         }).ToList();

                //IEnumerable<WCS_TO_WMS> EnumCross = from body in erplistWCS_TO_WMS
                //                                    where !lstWCS_TO_WMS1.Any(x => x.ITEMCODE == body.ITEMCODE)
                //                                    select body;//ERP商品比拣货订单多出来的
                //IEnumerable<WCS_TO_WMS> EnumCross2 = from body in lstWCS_TO_WMS1
                //                                     where !erplistWCS_TO_WMS.ToList<WCS_TO_WMS>().Any(x => x.ITEMCODE == body.ITEMCODE)
                //                                     select body;//拣货订单比ERP订单多出来的
                //if (EnumCross.ToList().Count > 0 || EnumCross2.ToList().Count > 0)
                if(ds==null || ds.Tables.Count<=0||ds.Tables[0].Rows.Count<=0)
                {
                    //return new ResultModel { RESULT = "failed", MESSAGE = "拣货完的商品信息与ERP订单商品信息不匹配，请联系管理员", DATA = null }; 
                    return new ResultModel { RESULT = "failed", MESSAGE = "ERP没有改订单商品信息，请联系管理员", DATA = null };
                }
                else
                { return new ResultModel { RESULT = "success", MESSAGE = "获取复核信息成功", DATA = lstWCS_TO_WMS1 }; }
            }
            catch (Exception ex)
            {
                return new ResultModel { RESULT = "failed", MESSAGE = "获取复核信息失败：" + ex.Message, DATA = null };
            }
        }


        [HttpGet]
        public ResultModel GetOrderUserInfo(string FBoxNO)
        {
            MB.Util.TraceEx.Write(FBoxNO);
            try
            {
                List<OrderInfo> lstWCS_TO_WMS = new BOrderPick().GetOrderInfoByFBoxNo(FBoxNO);
                if (lstWCS_TO_WMS == null || lstWCS_TO_WMS.Count <= 0)
                {
                    return new ResultModel { RESULT = "failed", MESSAGE = "当前泡沫箱未绑定发货订单", DATA = null };
                }
                return new ResultModel { RESULT = "success", MESSAGE = "获取订单用户信息成功", DATA = lstWCS_TO_WMS };
            }
            catch (Exception ex)
            {
                return new ResultModel { RESULT = "failed", MESSAGE = "获取订单用户信息失败：" + ex.Message, DATA = null };
            }
        }

        [HttpGet]
        public ResultModel GetPrintOrderUserInfo(string FBoxNO)
        {
            MB.Util.TraceEx.Write(FBoxNO);
            try
            {
                List<OrderInfo> lstWCS_TO_WMS = new BOrderPick().GetOrderInfoByFBoxNo(FBoxNO);
                if (lstWCS_TO_WMS == null || lstWCS_TO_WMS.Count <= 0)
                {
                    return new ResultModel { RESULT = "failed", MESSAGE = "当前泡沫箱未绑定发货订单", DATA = null };
                }
                foreach (OrderInfo item in lstWCS_TO_WMS)
                {
                    item.SaleAppItem = new BOrderPick().GetSaleAppItem1listByOrderNo(item.SHEETID);
                    item.SaleOrderItem = new BOrderPick().GetSaleOrderItem1listByOrderNo(item.SHEETID);
                }
                return new ResultModel { RESULT = "success", MESSAGE = "获取订单用户信息成功", DATA = lstWCS_TO_WMS };
            }
            catch (Exception ex)
            {
                return new ResultModel { RESULT = "failed", MESSAGE = "获取订单用户信息失败：" + ex.Message, DATA = null };
            }
        }

        [HttpGet]
        public ResultModel GetOrderInfoByTicket(string TicketNO)
        {
            MB.Util.TraceEx.Write(TicketNO);
            try
            {
                List<OrderInfo> lstWCS_TO_WMS = new BOrderPick().GetOrderInfoByTicketNO(TicketNO);

                return new ResultModel { RESULT = "success", MESSAGE = "根据票号获取订单信息成功", DATA = lstWCS_TO_WMS };
            }
            catch (Exception ex)
            {
                return new ResultModel { RESULT = "failed", MESSAGE = "根据票号获取订单信息失败：" + ex.Message, DATA = null };
            }
        }
    }
}
